jsp中<c:foreach>没有遍历

来源:百度知道 编辑:UC知道 时间:2024/05/31 06:28:14
代码如下,topic为一个实体bean,有author属性,为说明起见,author 赋0,1,2值,但是发现 <c:forEach>中没有遍历
<%
List<Topic> list = new ArrayList<Topic>();
Topic topic = new Topic();
for(int i= 0 ;i <3;i++)
{
topic.setAuthor(i+"");
list.add(topic);
System.out.println("author:" + topic.getAuthor());
}
request.setAttribute("topicTest",list);
%>
<c:forEach items="${requestScope.topicTest}" var="topic">
${topic.author}<br>
</c:forEach>

原因已找到:Topic topic = new Topic(); 应放到for 循环中去,要不然的话,之后的数据会覆盖之前的数据

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html&g